#include <QGuiApplication>
#include "./common/common.h"

#include "./business/database.h"


// 初始化HttpServer静态类成员
mg_serve_http_opts HttpServer::s_server_option;
std::string HttpServer::s_web_dir = "./web";
std::unordered_map<std::string, ReqHandler> HttpServer::s_handler_map;
std::unordered_set<mg_connection *> HttpServer::s_websocket_session_set;



bool handle_fun1(std::string url, std::string query, std::string body, mg_connection *c, OnRspCallback rsp_callback)
{
    // do sth
    std::cout << "handle fun1" << std::endl;
    std::cout << "query: " << query << std::endl;
    std::cout << "url: " << url << std::endl;
    std::cout << "body: " << body << std::endl;
    rsp_callback(c, "rsp1");
    return true;
}

bool handle_fun2(std::string url, std::string query, std::string body, mg_connection *c, OnRspCallback rsp_callback)
{
    // do sth
    std::cout << "handle fun2" << std::endl;
    std::cout << "url: " << url << std::endl;
    std::cout << "query: " << query << std::endl;
    std::cout << "body: " << body << std::endl;

    rsp_callback(c, "rsp2");

    return true;
}

int main(int argc, char *argv[]) 
{
    std::string port = "8001";
    auto http_server = std::shared_ptr<HttpServer>(new HttpServer);
    http_server->Init(port);
    http_server->AddHandler("/listDB", listDb);
    http_server->AddHandler("/open", openDb);
    http_server->AddHandler("/execSQL", execSQL);
    http_server->AddHandler("/api/fun1", handle_fun1);
    http_server->AddHandler("/api/fun2", handle_fun2);

    QCoreApplication app(argc,argv);

    http_server->Start();

    return app.exec();
}
